package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.FreeShippingRules;
import com.example.mall.dto.FreeShippingRulesDto;
import com.example.mall.mapper.FreeShippingRulesMapper;
import com.example.mall.query.FreeShippingRulesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IFreeShippingRulesService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 包邮规则表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class FreeShippingRulesServiceImpl extends ServiceImpl<FreeShippingRulesMapper, FreeShippingRules> implements IFreeShippingRulesService {

    @Autowired
    private FreeShippingRulesMapper freeShippingRulesMapper;

    @Override
    public PageDTO<FreeShippingRulesDto> queryPage(FreeShippingRulesQuery freeShippingRulesQuery) {
        if (freeShippingRulesQuery == null) {
            freeShippingRulesQuery = new FreeShippingRulesQuery();
        }
        Page<FreeShippingRules> page = freeShippingRulesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<FreeShippingRules> cartPage = lambdaQuery()
                .like(freeShippingRulesQuery.getRuleId()!= null, FreeShippingRules::getRuleId, freeShippingRulesQuery.getRuleId())
                .like(freeShippingRulesQuery.getDestination()!= null, FreeShippingRules::getDestination, freeShippingRulesQuery.getDestination())
                .like(freeShippingRulesQuery.getMinAmount()!= null, FreeShippingRules::getMinAmount, freeShippingRulesQuery.getMinAmount())
                .like(freeShippingRulesQuery.getMinWeight()!= null, FreeShippingRules::getMinWeight, freeShippingRulesQuery.getMinWeight())
                .like(freeShippingRulesQuery.getIsActive()!= null, FreeShippingRules::getIsActive, freeShippingRulesQuery.getIsActive())
                .orderByDesc(FreeShippingRules::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, FreeShippingRulesDto.class);
    }

    @Override
    public FreeShippingRules queryFreeShippingRulesById(Long ruleId) {
        AssertUtil.isNotNull(ruleId, "规则ID不能为空");
        return freeShippingRulesMapper.selectById(ruleId);
    }

    @Override
    public List<FreeShippingRules> queryFreeShippingRulesList() {
        return this.list();
    }

    @Override
    public void insert(FreeShippingRulesDto freeShippingRulesDto) {
        FreeShippingRules freeShippingRules = new FreeShippingRules();
        CopyUtils.copyProperties(freeShippingRulesDto, freeShippingRules);
        freeShippingRules.setRuleId(CodeGenerateUtils.generateUniqueId());
        freeShippingRules.setCreateDate(new Date());
        freeShippingRules.setUpdateDate(new Date());
        freeShippingRules.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        freeShippingRulesMapper.insert(freeShippingRules);
    }

    @Override
    public void update(FreeShippingRulesDto freeShippingRulesDto) {
        AssertUtil.isNotNull(freeShippingRulesDto.getRuleId(), "规则ID不能为空");
        FreeShippingRules freeShippingRules = freeShippingRulesMapper.selectById(freeShippingRulesDto.getRuleId());
        AssertUtil.isNotNull(freeShippingRules, "规则不存在");
        CopyUtils.copyProperties(freeShippingRulesDto, freeShippingRules);
        freeShippingRules.setUpdateDate(new Date());
        freeShippingRulesMapper.updateById(freeShippingRules);
    }

    @Override
    public void delete(Long ruleId) {
        AssertUtil.isNotNull(ruleId, "规则ID不能为空");
        FreeShippingRules freeShippingRules = freeShippingRulesMapper.selectById(ruleId);
        AssertUtil.isNotNull(freeShippingRules, "规则不存在");
        freeShippingRules.setIsActive(BaseConstants.LOSE_STATUS);
        freeShippingRules.setUpdateDate(new Date());
        freeShippingRulesMapper.updateById(freeShippingRules);
    }

    @Override
    public void deleteBatch(List<Long> ruleIds) {
        AssertUtil.isNotNull(ruleIds, "规则ID列表不能为空");
        List<FreeShippingRules> freeShippingRules = freeShippingRulesMapper.selectBatchIds(ruleIds);
        AssertUtil.isNotNull(freeShippingRules, "规则不存在");
        for (FreeShippingRules freeShippingRule : freeShippingRules) {
            freeShippingRule.setIsActive(BaseConstants.LOSE_STATUS);
            freeShippingRule.setUpdateDate(new Date());
            freeShippingRulesMapper.updateById(freeShippingRule);
        }
    }
}
